home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmiSoft / Dev / misc / WHDLoad_dev.lha / WHDLoad / Src / gci / winmem.c < prev   
Encoding:
C/C++ Source or Header  |  2003-07-12  |  8.9 KB  |  339 lines

  1. /*************************************************************************
  2. ;  :Module.    winmem.c
  3. ;  :Author.    Bert Jahn
  4. ;  :Address.    Franz-Liszt-Straße 16, Rudolstadt, 07404, Germany
  5. ;  :Version.    $Id: winmem.c 1.7 2003/07/12 17:08:46 wepl Exp wepl $
  6. ;  :History.    21.03.00 separated from main
  7. ;  :Copyright.    All Rights Reserved
  8. ;  :Language.    C
  9. ;  :Translator.    GCC
  10. *************************************************************************/
  11.  
  12. #include <stdlib.h>
  13. #include <string.h>
  14.  
  15. #include <exec/execbase.h>
  16. #include <libraries/mui.h>
  17. #include <mui/HexEdit_mcc.h>
  18.  
  19. #include <clib/alib_protos.h>
  20. #include <clib/gadtools_protos.h>
  21. #include <clib/muimaster_protos.h>
  22. #include <clib/utility_protos.h>
  23.  
  24. #include "whddump.h"
  25. #include "WHDLoadGCI.h"
  26.  
  27. /************************************************************************/
  28. /* Compiler Stuff                                                       */
  29. /************************************************************************/
  30.  
  31. #define REG(x) register __ ## x
  32. #define ASM    __asm
  33. #define SAVEDS __saveds
  34.  
  35. /************************************************************************/
  36. /* defines                                */
  37. /************************************************************************/
  38.  
  39. #define MAXWINMEM 10    /* amount of possible memory hexdump windows */
  40. #define MUIA_HexEdit_My    TAG_USER + 1
  41.  
  42. /************************************************************************/
  43. /* extern variables                            */
  44. /************************************************************************/
  45.  
  46. extern APTR            app,win;
  47. extern struct whddump_header    *header;
  48. extern struct Library        *MUIMasterBase;
  49. extern APTR            mem,emem,slave;
  50. extern const char* StringHexAccept;
  51.  
  52. /************************************************************************/
  53. /* static variables                            */
  54. /************************************************************************/
  55.  
  56. static APTR win_mem[MAXWINMEM];        /* memory hexdump windows */
  57. static APTR gad_hex[MAXWINMEM];        /* hex display gadgets */
  58. static APTR gad_goto[MAXWINMEM];    /* goto gadgets */
  59.  
  60. static struct MUI_CustomClass *HexClass;
  61.  
  62. static struct NewMenu MenuDataMem[] =
  63. {
  64.     MENUBASE
  65.     { NM_TITLE, "Memory"    , 0 ,0 ,0 ,(APTR)MEN_MEMORY    },\
  66.     { NM_ITEM ,  "Goto"    ,"G",0 ,0 ,(APTR)MEN_GOTO0    },\
  67.     { NM_END,NULL,0,0,0,(APTR)0 },
  68. };
  69.  
  70. /************************************************************************/
  71. /* function declarations                        */
  72. /************************************************************************/
  73.  
  74. void *    wmem_init    (void);
  75. void    wmem_finit    (void);
  76.  
  77. /************************************************************************/
  78.  
  79. struct HexData {
  80.     int low_bound;
  81.     int high_bound;
  82.     int base_address;
  83.     int len_base;
  84.     int len_off;
  85. };
  86.  
  87. SAVEDS ULONG
  88. mNew(
  89.     struct IClass *cl,
  90.     Object *obj,
  91.     Msg msg
  92. ) {
  93.     struct TagItem *tags,*tag;
  94.     int i;
  95.     struct HexData tmp = {0,0,0,0,0};
  96.  
  97.     for (tags=((struct opSet *)msg)->ops_AttrList; (tag=NextTagItem(&tags)); )
  98.     {
  99.         switch (tag->ti_Tag)
  100.         {
  101.             case MUIA_HexEdit_LowBound: tmp.low_bound = tag->ti_Data; break;
  102.             case MUIA_HexEdit_HighBound: tmp.high_bound = tag->ti_Data; break;
  103.             case MUIA_HexEdit_BaseAddressOffset: tmp.base_address = tag->ti_Data; break;
  104.             case MUIA_HexEdit_AddressChars:
  105.                 i = tmp.high_bound + tmp.base_address;
  106.                 tmp.len_base =    i < 0x10000 ? 4 :
  107.                                 i < 0x100000 ? 5 :
  108.                                 i < 0x1000000 ? 6 :
  109.                                 i < 0x10000000 ? 7 : 8;
  110.                 if (tmp.low_bound + tmp.base_address) {
  111.                     i = tmp.high_bound - tmp.low_bound;
  112.                     tmp.len_off =    i < 0x1000 ? 3 :
  113.                                     i < 0x10000 ? 4 :
  114.                                     i < 0x100000 ? 5 : 6;
  115.                 } else {
  116.                     tmp.len_off = 0;
  117.                 }
  118.                 tag->ti_Data = tmp.len_base + (tmp.len_off ? 1 + tmp.len_off : 0);
  119.                 break;
  120.         }
  121.     }
  122.  
  123.     if (!(obj = (Object *)DoSuperMethodA(cl,obj,msg))) return(0);
  124.  
  125.     memcpy(INST_DATA(cl,obj), &tmp, sizeof(tmp));
  126.  
  127.     return ((ULONG)obj);
  128. }
  129.  
  130. SAVEDS ULONG
  131. mSet(
  132.     struct IClass *cl,
  133.     Object *obj,
  134.     Msg msg
  135. ) {
  136.     struct TagItem *tags,*tag;
  137.     char *s;
  138.     int i;
  139.  
  140.     for (tags=((struct opSet *)msg)->ops_AttrList; (tag=NextTagItem(&tags)); )
  141.     {
  142.         switch (tag->ti_Tag)
  143.         {
  144.             case MUIA_HexEdit_My:
  145.                 get( gad_goto[tag->ti_Data], MUIA_String_Contents, &s );
  146.                 i = atoi( s );
  147.                 set( obj, MUIA_HexEdit_CursorAddress, 1000 );
  148.                 break;
  149.         }
  150.     }
  151.  
  152.     return DoSuperMethodA(cl,obj,msg);
  153. }
  154.  
  155. SAVEDS ULONG
  156. mCreateDisplayAddress(
  157.     struct IClass *cl,
  158.     Object *obj,
  159.     struct MUIP_HexEdit_CreateDisplayAddress *msg
  160. ) {
  161.     struct HexData *d = INST_DATA(cl,obj);
  162.     ULONG address;
  163.     UBYTE i;
  164.     UBYTE *hextable = "0123456789ABCDEF";
  165.  
  166.     address = (d->base_address + msg->address) << (32 - d->len_base * 4);
  167.     for(i = 0; i < d->len_base; i++)
  168.     {
  169.         *(*msg->cp)++ = hextable[address >> (32 - 4)];
  170.         address <<= 4;
  171.     }
  172.  
  173.     address = (msg->address - d->low_bound) << (32 - d->len_off * 4);
  174.     if (d->len_off) {
  175.         *(*msg->cp)++ = (unsigned char) '·';
  176.         for(i = 0; i < d->len_off; i++)
  177.         {
  178.             *(*msg->cp)++ = hextable[address >> (32 - 4)];
  179.             address <<= 4;
  180.         }
  181.     }
  182.  
  183.     return(TRUE);
  184. }
  185.  
  186. /*
  187. SAVEDS ASM ULONG
  188. HexDispatcher(
  189.     REG(a0) struct IClass *cl,
  190.     REG(a2) Object *obj,
  191.     REG(a1) Msg msg
  192. */
  193. SAVEDS ULONG
  194. HexDispatcher(
  195.     struct IClass *cl __asm("a0"),
  196.     Object *obj __asm("a2"),
  197.     Msg msg __asm("a1")
  198. ) {
  199.     switch (msg->MethodID)
  200.     {
  201.         case OM_NEW                : return(mNew            (cl,obj,(APTR)msg));
  202.         case OM_SET                : return(mSet            (cl,obj,(APTR)msg));
  203.         case MUIM_HexEdit_CreateDisplayAddress    : return(mCreateDisplayAddress    (cl,obj,(APTR)msg));
  204.     }
  205.     return(DoSuperMethodA(cl,obj,msg));
  206. }
  207.  
  208. /****************************************************************************/
  209.  
  210. void * wmem_init() {
  211.     return HexClass = MUI_CreateCustomClass(NULL,MUIC_HexEdit,NULL,sizeof(struct HexData),HexDispatcher);
  212. }
  213.  
  214. void wmem_finit() {
  215.     if (HexClass) MUI_DeleteCustomClass(HexClass);
  216. }
  217.  
  218. /****************************************************************************/
  219.  
  220. void
  221. wmem_make(
  222.     int adr        /* start address (logical) of memory to display */
  223. ) {
  224.     int n;
  225.     APTR prop;
  226.     ULONG open;
  227.     APTR low,high;
  228.     ULONG off,cur;
  229.     char *title;
  230.     
  231.     /*
  232.      *  check if there is a free window left
  233.      *  if window closed dispose it
  234.      */
  235.     for (n=0; (n<MAXWINMEM) && win_mem[n]; n++) {
  236.         get(win_mem[n],MUIA_Window_Open,&open);
  237.         if (!open) {
  238.             DoMethod(app,OM_REMMEMBER,win_mem[n]);
  239.             MUI_DisposeObject(win_mem[n]);
  240.             win_mem[n] = NULL;
  241.             break;
  242.         }
  243.     }
  244.     if (n==MAXWINMEM) {
  245.         MUI_Request(app,win,0,NULL,"Ok","Sorry, too many windows already open.");
  246.         return;
  247.     }
  248.     
  249.     /*
  250.      *  check which memory is requested
  251.      */
  252.     if (adr < header->wdh_BaseMemSize) {
  253.         title = "Base Memory (Chip)";
  254.         low = mem;
  255.         high = ((UBYTE*)mem) + header->wdh_BaseMemSize - 1;
  256.         off = -(ULONG)mem;
  257.         cur = adr;
  258.     } else if ((adr >= header->wdh_ExpMemLog) && (adr < header->wdh_ExpMemLog + header->wdh_ExpMemLen)) {
  259.         title = "Expansion Memory (Fast)";
  260.         low = emem;
  261.         high = ((UBYTE*)emem) + header->wdh_ExpMemLen - 1;
  262.         off = header->wdh_ExpMemLog - (ULONG)emem;
  263.         cur = adr - header->wdh_ExpMemLog;
  264.     } else if ((adr >= header->wdh_SlaveLog) && (adr < header->wdh_SlaveLog + header->wdh_SlaveLen)) {
  265.         title = "Slave Memory";
  266.         low = slave;
  267.         high = ((UBYTE*)slave) + header->wdh_SlaveLen - 1;
  268.         off = header->wdh_SlaveLog - (ULONG)slave;
  269.         cur = adr - header->wdh_SlaveLog;
  270.     } else {
  271.         MUI_Request(app,win,0,NULL,"Ok","Sorry, address is outside the defined memory.");
  272.         return;
  273.     }
  274.  
  275.     /*
  276.      *  create the window
  277.      */
  278.     win_mem[n] = WindowObject,
  279.         MUIA_Window_Title, title,
  280.         MUIA_Window_ID   , n + MAKE_ID('M','E','M','0'),
  281.         MUIA_Window_Menustrip, MUI_MakeObject(MUIO_MenustripNM,MenuDataMem,0),
  282.         WindowContents, VGroup,
  283.             Child, HGroup,
  284.                 Child, Label2("Goto Offset:"),
  285.                 Child, gad_goto[n] = BetterStringObject,
  286.                     StringFrame,
  287.                     MUIA_String_Accept , StringHexAccept,
  288.                     MUIA_String_MaxLen , 10,
  289.                     MUIA_String_Format , MUIV_String_Format_Right,
  290.                     MUIA_ShortHelp, "Goto Offset",
  291.                     MUIA_String_Contents, "$",
  292.                     End,
  293.                 End,
  294.             Child, HGroup,
  295.                 Child, gad_hex[n] = NewObject(HexClass->mcc_Class,0, VirtualFrame,
  296.                     MUIA_HexEdit_LowBound, low,
  297.                     MUIA_HexEdit_HighBound, high,
  298.                     MUIA_HexEdit_BaseAddressOffset, off,
  299.                     MUIA_HexEdit_AddressChars, 0,    /* must be after Low, High and BaseAddressOffset! */
  300.                     MUIA_HexEdit_EditMode, FALSE,
  301.                     MUIA_HexEdit_SelectMode, MUIV_HexEdit_SelectMode_Byte,
  302.                     MUIA_HexEdit_CursorAddress, cur,
  303.                     MUIA_CycleChain, 1,
  304.                     End,
  305.                 Child, prop = ScrollbarObject,
  306.                     MUIA_Prop_UseWinBorder, MUIV_Prop_UseWinBorder_Right,
  307.                     End,
  308.                 End,
  309.             End,
  310.         End;
  311.     if (!win_mem[n]) {
  312.         MUI_Request(app,win,0,NULL,"Ok","Couldn't open window.");
  313.         return;
  314.     }
  315.  
  316.     set(gad_hex[n],MUIA_HexEdit_BaseAddressOffset,off);
  317.     set(gad_hex[n],MUIA_HexEdit_AddressChars,2);
  318.  
  319.     /*
  320.      *  add window to application and open it
  321.      */
  322.     DoMethod(app,OM_ADDMEMBER,win_mem[n]);
  323.     set(gad_hex[n],MUIA_HexEdit_PropObject,prop);
  324.     DoMethod(win_mem[n],MUIM_Notify,MUIA_Window_CloseRequest,TRUE,win_mem[n],3,MUIM_Set,MUIA_Window_Open,FALSE);
  325.     DoMethod(gad_goto[n],MUIM_Notify,MUIA_String_Accept,TRUE,gad_hex[n],3,MUIM_Set,MUIA_HexEdit_My,n);
  326.     set(win_mem[n],MUIA_Window_Open,TRUE);
  327.  
  328. }
  329.  
  330. /****************************************************************************/
  331.  
  332. void wmem_goto ( int num )
  333. {
  334.  
  335. }
  336.  
  337. /****************************************************************************/
  338.  
  339.